<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Client extends CI_Controller {


        private $server = 'http://localhost/rest-service-excursions/restServerExcursions/service/';
        private $hosting = 'http://www.it-montenegro.com/demo/excursiontours-pro/';
        private $data = array();
        private $syslang;

        public function __construct(){

            parent::__construct();

            $this->load->library('rest', array(  
                    'server' => $this->server
                ));

            $this->data['url'] = base_url();
            $this->data['hosting'] = $this->hosting;
            $this->syslang = $this->lang->lang();
            $this->data['local_lang'] = $this->syslang;
            
            //urlify library for prety url
            $linktoclass = "urlify.php";
            require_once($linktoclass);
            
        }

        public function index()
        { 

            redirect(base_url('excursions'));
            //$this->get_all();

            //echo $this->curl->simple_get($this->server.'excursions?eb_starting=Tuesday&eb_freetext=&exc_sort=none');

            // Debug data ------------------------------------------------
            //print_r($this->curl->info);
        }

        public function get_all(){

            $this->data['title'] = 'Welcome | Online Booking';

            $params = array(
                'eb_starting' => isset($_POST['eb_starting']) ? $_POST['eb_starting'] : 'Any',
                'eb_freetext' => isset($_POST['eb_freetext']) ? $_POST['eb_freetext'] : '',
                'exc_sort' => isset($_POST['exc_sort']) ? $_POST['exc_sort'] : 'none',
                'lang' => $this->syslang
            );

            //$this->firephp->log($params);

            $this->data['excursions'] = $this->rest->post('excursions', $params, 'json');

            //print_r($data['excursions']);

            $this->layout->view('booking/home', $this->data);

        }

        public function details($exc){

            /* Decode, explode, get last */
            
            $excursion_ID = urldecode($exc);
            $excursion_ID = explode('-',$excursion_ID);
            $excursion_ID = $excursion_ID[count($excursion_ID)-1];
            
            $params = array(
                'excursion_id' => $excursion_ID,
                'lang' => $this->syslang
            );

            $this->data['excursion'] = $this->rest->get('details', $params, 'json');
            $this->data['gallery'] = $this->rest->get('gallery', $params, 'json');

            //print_r($this->data['excursion']);
            //print_r($this->data['gallery']);

            $this->data['title'] = $this->data['excursion']->title;

            $this->layout->view('booking/details', $this->data); 

        }

        public function check_available(){

            //print_r($_POST);

            $params = array(
                'excid' => $_POST['exc_id'],
                'date' => strtotime($_POST['exc_date']),
                'noadult' => $_POST['adults'],
                'noch' => $_POST['children'],
                'persons' => $_POST['adults'] + $_POST['children'],       
                'adultprice' => $_POST['adultprice'],        
                'chprice' => $_POST['chprice'],        
                'totalprice' => $_POST['totalprice'], 
                'lang' => $this->syslang
            );

            //print_r($params);

            $is_available = $this->rest->post('check_capacity', $params, 'json');

            //check if booking is available
            if($is_available->available){

                redirect(base_url('customer/'.$is_available->book_info));

            }else{

                $this->data['booking_msg'] = '<b>Booking is not available.</b><br />';   
                $this->data['booking_msg'] .= 'Place left <b>'.$is_available->place_left.'</b>.';

                $params = array(
                    'excursion_id' => $_POST['id'],
                    'lang' => $this->syslang
                );

                $this->data['excursion'] = $this->rest->get('details', $params, 'json');
                $this->data['gallery'] = $this->rest->get('gallery', $params, 'json');

                $this->data['title'] = $this->data['excursion']->title;
                $this->layout->view('booking/details', $this->data); 

            }

        }

        public function customer($book_info_ID){

            $this->data['title'] = "Customer Information";

            $params = array(
                'bid' => $book_info_ID,
                'lang' => $this->syslang
            );

            $this->data['booking_info'] = $this->rest->get('booking_info', $params, 'json');

            //print_r($this->data['booking_info']);

            $this->layout->view('booking/customer', $this->data); 

        }

        public function book_now(){

            //print_r($_POST);

            $booking = $this->rest->post('do_booking', $_POST, 'json');

            //check if booking is insert in db
            if($booking->success){

                redirect(base_url('booking/'.$booking->booking_id));

            }else{

                $this->data['booking_msg'] = '<b>Booking error.</b><br />';   
                $this->data['booking_msg'] .= 'Please try again.';
                $this->data['title'] = "Customer Information";

                $params = array(
                    'bid' => $_POST['excid'],
                    'lang' => $this->syslang
                );

                $this->data['booking_info'] = $this->rest->get('booking_info', $params, 'json');
                $this->layout->view('booking/customer', $this->data); 

            }

        }

        public function booking($booking_ID){
            
            $this->data['title'] = "Booking Information";

            $params = array(
                'fbid' => $booking_ID,
                'lang' => $this->syslang
            );

            $this->data['booking'] = $this->rest->get('total', $params, 'json');

            //print_r($this->data['booking']);

            $this->layout->view('booking/total', $this->data);

        }

    }

    /* End of file client.php */
    /* Location: ./application/controllers/client.php */